package com.louis.web.propertyeditor;

import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.util.StringUtils;

import com.louis.dao.GeneralDao;
import com.louis.domain.DomainObject;

import java.beans.PropertyEditorSupport;

/**
 * @author louisjiang
 * @Date: Jun 1, 2011
 * @Time: 6:12:34 PM
 */

@Configurable(autowire = Autowire.BY_NAME)
public class DomainObjectPropertyEditor extends PropertyEditorSupport {

	private Class<? extends DomainObject>	clazz;

	@Autowired(required = true)
	private GeneralDao						generalDao;

	public DomainObjectPropertyEditor(Class<? extends DomainObject> clazz) {
		this.clazz = clazz;
	}

	public DomainObjectPropertyEditor(Class<? extends DomainObject> clazz, GeneralDao generalDao) {
		this.clazz = clazz;
		this.generalDao = generalDao;
	}

	@Override
	public String getAsText() {
		DomainObject domainObject = (DomainObject) getValue();
		return domainObject == null ?"": domainObject.getId() + "";
	}

	@Override
	public void setAsText(String id) throws IllegalArgumentException {
		if (StringUtils.hasText(id)) {
			// setValue(getGenericDao().get(id, clazz));
		} else {
			setValue(null);
		}
	}

	public GeneralDao getGenericDao() {
		return generalDao;
	}

	@Autowired(required = true)
	public void setGenericDao(GeneralDao genericDao) {
		this.generalDao = genericDao;
	}

	public Class<? extends DomainObject> getClazz() {
		return clazz;
	}

	public void setClazz(Class<? extends DomainObject> clazz) {
		this.clazz = clazz;
	}

	public GeneralDao getGeneralDao() {
		return generalDao;
	}

	@Autowired(required = true)
	public void setGeneralDao(GeneralDao generalDao) {
		this.generalDao = generalDao;
	}

}